define([ "app", "jquery", "md5", "ecb","artDialog","iframeTools", "services/UserService",'directives/ImageCrop',
		"resources/UserResource","resources/ContactsResource", "resources/ShowTimesUserResource" ],
		function(app) {

			var deps = [ "$scope", "$state", "$window", "$location", "$stateParams", "UserService", "UserResource","ContactsResource", "ShowTimesUserResource" ];

			function controller($scope, $state, $window, $location, $stateParams, UserService, UserResource,ContactsResource, ShowTimesUserResource) {
				
				var imageData;
				
				$scope.saveUser = function() {

					if (imageData && imageData.file) {
	                    uploadImage().success(function(result) {
	                        $scope.userVo.userPicUrl = eval("(" + result.fileId + ")").obj;
	                        saveOrUpdate();
	                    });
	                } else {
	                	saveOrUpdate();
	                }
				};

				
				 /**
	             * 图片数据
	             */
	            $scope.$bus.subscribe({
	                channel: 'image',
	                topic: 'crop',
	                callback: function(data) {
	                    imageData = data;
	                }
	            });
	            
				/*$scope.close = function($scope,ShowTimesUserResource) {
					$modalInstance.close();
				};*/
				
				$scope.close = function() {
					$state.go('home.userManagement');
				}
				
				
				function saveOrUpdate(){
					var password =encrypt($scope.userVo.password, $scope.userVo.userName);
					$scope.userVo.password=password;
					if ($scope.userCreateForm.$invalid) {
						return false;
					}
					$scope.userCreateForm.$invalid = true;
					
					ShowTimesUserResource.save($scope.userVo).success(function(data){
						$scope.userCreateForm.$invalid = false;
						if (data.resCode=='0000') {
							//$modalInstance.close($scope.userVo);
						}else{
							art.dialog.alert("新增用户失败");
							//$modalInstance.close($scope.userVo);
						}
						$state.go('home.userManagement');
		            });
					
				}
				
				/**
	             * 图片剪裁
	             */
	            function uploadImage() {
	                var formData = new FormData();
	                formData.append("file", imageData.file);

	                var cropParam = {
	                    cropX: imageData.cropX || 0,
	                    cropY: imageData.cropY || 0,
	                    cropW: imageData.cropW || 0,
	                    cropH: imageData.cropH || 0
	                };
	                return ContactsResource.uploadCropImage(formData, cropParam);
	            }
	            
				/**
				 * 初始化下拉列表
				 *//*
				function initDate(AddYokiUserResource){
					
					AddYokiUserResource.queryOrgList(0).success(function(data){
						$scope.orgList=data;
		            });
					//$scope.selected=$scope.orgList[0].companyCode;
					//$scope.companyVo.companyCode=$scope.orgList[0].companyCode;
				}*/
			}
			
			/**
			 * 密码 encrypt 加密
			 */
			function encrypt(content,srcKey){
			      var keyLen = 8;
				  var srcKeyLen = srcKey.length;
				
				 if(srcKeyLen > keyLen){
					 srcKey = srcKey.substr(0,keyLen);
					
				 }else{
					 var paddLen = keyLen -  srcKeyLen;
					 for(var i=0;i<paddLen;i++){
						 srcKey += "0";
					 }
				 }
				
				 var key = CryptoJS.enc.Utf8.parse(srcKey);		
				 var encryptStr =  CryptoJS.DES.encrypt(content, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
				return encryptStr.toString();
			}
			
			controller.$inject = deps;
			return app.lazy.controller("AddUserController", controller);
		});
